package com.zhu.blog.controller.admin;



import com.zhu.blog.model.Blog;
import com.zhu.blog.model.User;
import com.zhu.blog.service.BlogService;
import com.zhu.blog.service.TagService;
import com.zhu.blog.service.TypeService;
import com.zhu.blog.dto.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;



@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    //分页展示博客的信息
    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                BlogQuery blog, Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs";
    }

    //搜索展示出来的内容
    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQuery blog, Model model){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs :: blogList";
    }

    //跳转博客新增页面
    @GetMapping("/blogs/input")
    public String blogsinput(Model model){
        model.addAttribute("blog",new Blog());
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        return "admin/blogs-input";
    }

    //新增保存方法
    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session){
        blog.setUser((User)session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        Blog b = blogService.saveBlog(blog);
        if(b == null){
            attributes.addFlashAttribute("message","新增失败");
        }else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/blogs";
    }

    //获取要修改的博客id
    @GetMapping("/blogs/{id}/input")
    public String editBlogs(@PathVariable Long id, Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        //先获取要修改的id
        Blog blog = blogService.getBlog(id);
        //初始化该id的内容，将标签数组转换为字符串
        blog.init();
        model.addAttribute("blog",blog);
        return "admin/blogs-input";
    }

    //修改博客内容
    @PostMapping("/blogs/{id}")
    public String post( Blog blog,@PathVariable Long id,HttpSession session, RedirectAttributes attributes){
        blog.setUser((User)session.getAttribute("user"));
        Blog b = blogService.updateBlog(id, blog);
            if(b == null){
                attributes.addFlashAttribute("message","修改失败!");
            }else {
                attributes.addFlashAttribute("message","修改成功!");
            }
            return "redirect:/admin/blogs";
    }

    //删除博客
    @GetMapping("/blogs/{id}/delete")
    public String deleteBlog(@PathVariable Long id,RedirectAttributes attributes){
            blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/blogs";

    }
}
